package com.ihrm.salarys.service;

import com.ihrm.common.utils.IdWorker;
import com.ihrm.domain.atte.entity.ArchiveMonthlyInfo;
import com.ihrm.domain.salarys.SalaryArchive;
import com.ihrm.domain.salarys.SalaryArchiveDetail;
import com.ihrm.domain.salarys.Settings;
import com.ihrm.domain.salarys.UserSalary;
import com.ihrm.domain.social_security.ArchiveDetail;
import com.ihrm.salarys.dao.ArchiveDao;
import com.ihrm.salarys.dao.ArchiveDetailDao;
import com.ihrm.salarys.dao.UserSalaryDao;
import com.ihrm.salarys.feign.FeignClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service
public class ArchiveService {
    
    /**
     * 归档主表dao
     */
    @Autowired
    private ArchiveDao archiveDao;
    
    /**
     * 归档明细表dao
     */
    @Autowired
    private ArchiveDetailDao archiveDetailDao;
    
    /**
     * 员工薪资设置dao
     */
    @Autowired
    private UserSalaryDao userSalaryDao;
    
    @Autowired
    private IdWorker idWorker;
    
    /**
     * 员工薪资service
     */
    @Autowired
    private SalaryService salaryService;
    
    /**
     * 福利津贴service
     */
    @Autowired
    private SettingsService settingsService;
    
    @Autowired
    private FeignClientService feignClientService;
    
    /**
     * 根据企业和年月查询归档主表数据
     */
    public SalaryArchive findSalaryArchive(String yearMonth, String companyId) {
        return archiveDao.findByCompanyIdAndYearsMonth(companyId,yearMonth);
    }
    
    /**
     * 根据归档的id查询所有的归档明细记录
     */
    public List<SalaryArchiveDetail> findSalaryArchiveDetail(String id) {
        return archiveDetailDao.findByArchiveId(id);
    }
    
    /**
     * 根据归档的id查询所有的归档明细记录
     */
    
    /**
     * 计算薪资 : 社保和考勤已经归档
     */
    public List<SalaryArchiveDetail> getReports(String yearMonth, String companyId) {
        List<SalaryArchiveDetail> list = new ArrayList<>();
    
        /**
         * 查询当前企业的福利津贴
         */
        Settings settings = settingsService.findById(companyId);
    
        /**
         * 查询所有的用户
         */
        Page<Map> page = userSalaryDao.findPage(companyId, null);
        
        /**
         * 遍历用户数据
         */
        for (Map map : page.getContent()) {
            
            /**
             * 构造SalaryArchiveDetail
             */
            SalaryArchiveDetail detail = new SalaryArchiveDetail();
            /**
             * 构造用户数据
             */
            detail.setUser(map);
            
            /**
             * 获取每个用户的社保数据
             */
            ArchiveDetail socialInfo = feignClientService.getSocialInfo(detail.getUserId(), yearMonth);
            detail.setSocialInfo(socialInfo);
            
            /**
             * 获取每个用户的考勤数据
             */
            ArchiveMonthlyInfo atteInfo = feignClientService.getAtteInfo(detail.getUserId(), yearMonth);
            detail.setAtteInfo(atteInfo);
            
            /**
             * 获取每个用户的薪资
             */
            UserSalary userSalary = salaryService.findUserSalary(detail.getUserId());
            detail.setUserSalary(userSalary);
            
            /**
             * 计算工资
             */
            detail.calSalary(settings);
            list.add(detail);
        }
        
        return list;
    }
    
}
